<?php
namespace app\api\model;
use plugin\filter;
use think\Db;
use think\Model;
use app\api;

/*用户总机模型*/
class UserTrace extends CommonModel
{
    private $table_name = 'user_trace';
    public function __construct($args = [])
    {
        parent::__construct($args);
    }

    public function defaultParam($args)
    {
        $data = [
            'ipcookie' => real_ip(),
            'url'      => "",
            'info'     => "",
            'add_time'  => time()
        ];
        return array_merge($data,$args);
    }

    //新增
    public function add($args)
    {
        $data = $this->defaultParam($args);
        if(empty($data['url']) || !filter::regex_true($data['url'],"url"))
        {
            return ['error'=> 1 ,'msg'=> '参数错误'];
        }
        $data['info'] = strip_tags($data['info']);

        $data = resetArr($data,['ipcookie','url','info','add_time']);

        db($this->table_name)->insertGetId($data);

        return ['error'=>0,'msg'=>"设置完毕"];
    }

    //根据ip，获取今天的追踪记录
    public function getToday($args)
    {
        if(!isset($args['ip']) || empty($args['ip']))
        {
            return ['error'=>1,'msg'=>'缺少ip'];
        }
        $date0 = strtotime(date('Y-m-d',time()));
        $date24 = strtotime(date('Y-m-d',strtotime('+1 day')));
        $where = [
            'ipcookie'    => $args['ip'],
            'add_time' => [['>',$date0],['<',$date24]]
        ];
        $data =  db($this->table_name)->where($where)->order('add_time DESC')->select();
        if(!empty($data))
        foreach($data as $k=>$v)
        {
            $data[$k]['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
        }
        return $data;
    }
}